﻿using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utils;

namespace BLL
{
    /// <summary>
    /// 图书管理业务类
    /// </summary>
    public class BookManagementBLL
    {
        BookManagementDAL bookManagementDAL = new BookManagementDAL();
        /// <summary>
        /// 图书管理
        /// </summary>
        /// <returns></returns>
        public DataTable Show()
        {

            return bookManagementDAL.ShowDAL();
        }

        /// <summary>
        /// 获取子类
        /// </summary>
        /// <returns></returns>
        public DataTable FindCategory()
        {
            return bookManagementDAL.FindsonDAL();
        }

        /// <summary>
        /// 获取父类
        /// </summary>
        /// <returns></returns>
        public DataTable FindParent()
        {
            return bookManagementDAL.FindParentDAL();
        }

        /// <summary>
        /// 图书列表及搜索
        /// </summary>
        /// <param name="bookid">图书id</param>
        /// <returns></returns>
        public DataTable getBookbyid(string bookid)
        {
            //List<BookInfo> bookInfos = ListTable.DataTableToList<BookInfo>(bookManagementDAL.getBookbyidDAL(""));
            //ListTable.DataTableToList<object>(bookManagementDAL.getBookbyidDAL(""));
            return bookManagementDAL.getBookbyidDAL(bookid);
        }

        /// <summary>
        /// 添加图书
        /// </summary>
        /// <param name="book">图书信息</param>
        /// <returns></returns>
        public string AddBook(BookInfo book)
        {
            if (bookManagementDAL.getByBookidDAL(book._bookId).Rows.Count<1)
            {
                if (bookManagementDAL.AddBookDAL(book) > 0)
                {
                    return EnumTools.GetDescription(ManageEnums.DOCUMENT.Add_SUCCEED);
                }
                else
                {
                    return EnumTools.GetDescription(ManageEnums.DOCUMENT.Add_LOSE);
                }
            }
            return EnumTools.GetDescription(ManageEnums.BORROW.EXIST);
        }

        /// <summary>
        /// 修改图书
        /// </summary>
        /// <param name="book"></param>
        /// <returns></returns>
        public string UpdateBook(BookInfo book)
        {
            //查询当然修改图书是否被借出
            string str= Judge(book._Id.ToString());
            if (str== null)
            {
                if (bookManagementDAL.UpdateBookDAL(book) > 0)
                {
                    return EnumTools.GetDescription(ManageEnums.DOCUMENT.Update_SUCCEED);
                }
                else
                {
                    return EnumTools.GetDescription(ManageEnums.DOCUMENT.Update_LOSE);
                }
            }
            return str;

        }

        /// <summary>
        /// 删除图书
        /// </summary>
        /// <param name="bookid">图书id</param>
        /// <returns></returns>
        public string DeleteBook(string bookid,string id)
        {
            //查询当然修改图书是否被借出
            if (Judge(id)== null)
            {
              
                if (bookManagementDAL.DeleteBookDAL(bookid) > 0)
                {
                    return EnumTools.GetDescription(ManageEnums.DOCUMENT.Datate_SUCCEED);
                }
                else
                {
                    return EnumTools.GetDescription(ManageEnums.DOCUMENT.Datate_LOSE);
                }
            }
            return Judge(id);

        }

        /// <summary>
        /// 查询当然修改图书是否被借出
        /// </summary>
        /// <param name="bookid">图书id</param>
        /// <returns></returns>
        private string Judge(string bookid)
        {
            DataTable dt = bookManagementDAL.getBookBorrowDAL(bookid);
            if (dt.Rows.Count >= 1)
            {
                return EnumTools.GetDescription(ManageEnums.BORROW.LEND);
            }
            return null;
        }

        /// <summary>
        /// 树目录搜索
        /// </summary>
        /// <param name="CategoryID"></param>
        /// <returns></returns>
        public DataTable getBookCategoryID(string CategoryID)
        {

            return bookManagementDAL.getBookCategoryDAL(CategoryID);
        }
    }
}
